import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreMemberCashOutService extends BaseService<any> {
  private readonly logger = new Logger(CoreMemberCashOutService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * transferFinish
   * 对应 PHP: CoreMemberCashOutService_core::transferFinish()
   * 逻辑类型: undefined - undefined
   */
  async transferFinish(site_id: any, transfer_no: any) {
    // 基于PHP真实逻辑: transferFinish
    // PHP原文: $cash_out = $this->model->where(             [                 ['site_id', '=', $site_id],                 ['transfer_no', '=', $transfer_no]         ...
const cash_out = this.model.where(
            [
                ['site_id', '=', site_id],
                ['transfer_no', '=', transfer_no)
            )
        ).findOrEmpty(];

        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_TRANSFER  && cash_out.status != MemberCashOutDict.TRANSFER_ING) throw new BusinessException('CASHOUT_STATUS_NOT_IN_WAIT_TRANSFER'];
        //减去提现中金额
        this.give(site_id, cash_out);
        cash_out.save([
            status: MemberCashOutDict.TRANSFERED,
            transfer_time: time()
        ]];

        return true;
    }

    /**
     * 累加提现金额,累减提现中金额
     * @param int site_id
     * @param MemberCashOut cash_out
     * @return true
     */
    async give(int site_id, MemberCashOut cash_out)
    {
        const core_member_service = this.coreMemberService;
        const member = core_member_service.find(site_id, cash_out.member_id);
        if (member.isEmpty()) throw new BusinessException('MEMBER_NOT_EXIST'];
        member.save(
            [
                cash_out.account_type . _cash_outing: member[cash_out.account_type  + '_cash_outing') - cash_out.apply_money
            )
        ];
        return true;
    }

    /**
     * 获取对象
     * @param int site_id
     * @param int id
     * @return MemberCashOut|array|mixed|Model
     */
    async find(int site_id, int id)
    {
        return this.model.where([
            ['site_id', '=', site_id],
            ['id', '=', id),
        )).findOrEmpty(];
    }

    /**
     * 申请提现
     * @param int site_id
     * @param int member_id
     * @param data
     * @return true
     */
    async apply(int site_id, int member_id, data)
    {
        const core_member_service = this.coreMemberService;
        const member = core_member_service.find(site_id, member_id);

        if (member.isEmpty()) throw new BusinessException('MEMBER_NOT_EXIST'];
        const config = this.coreMemberConfigService.getCashOutConfig(site_id);
        const is_open = config.is_open;
        if (is_open == 0) throw new BusinessException('CASHOUT_NOT_OPEN');
        const apply_money = data.apply_money;
        if (apply_money < config.min) throw new BusinessException('CASHOUT_MONEY_TOO_LITTLE');
        const transfer_type = data.transfer_type;
        if (!in_[transfer_type, config.transfer_type)) throw new BusinessException('CASHOUT_TYPE_NOT_OPEN'];
        const service_money = format_round_money(apply_money * config.rate / 100);
        const min = config.min;
        if (apply_money < min) throw new BusinessException('CASHOUT_MONEY_TOO_LITTLE'];
//        apply_money, transfer_type, transfer_realname, transfer_mobile, transfer_bank, transfer_account
        const money = apply_money - service_money;
        const account_type = data.account_type || MemberAccountTypeDict.MONEY;

        const cash_out_account = [);
        if (transfer_type != TransferDict.WECHAT) {
            const cash_out_account = this.coreMemberCashOutAccountService.getInfo(data.account_id, site_id, member_id);
            if (!cash_out_account)) throw new BusinessException('CASH_OUT_ACCOUNT_NOT_EXIST');
  }

  /**
   * give
   * 对应 PHP: CoreMemberCashOutService_core::give()
   * 逻辑类型: undefined - undefined
   */
  async give(site_id: number, cash_out: number) {
    // 基于PHP真实逻辑: give
    // PHP原文: $core_member_service = new CoreMemberService();         $member = $core_member_service->find($site_id, $cash_out->member_id);         if ($member->isE...
const core_member_service = this.coreMemberService;
        const member = core_member_service.find(site_id, cash_out.member_id);
        if (member.isEmpty()) throw new BusinessException('MEMBER_NOT_EXIST'];
        member.save(
            [
                cash_out.account_type . _cash_outing: member[cash_out.account_type  + '_cash_outing') - cash_out.apply_money
            )
        ];
        return true;
    }

    /**
     * 获取对象
     * @param int site_id
     * @param int id
     * @return MemberCashOut|array|mixed|Model
     */
    async find(int site_id, int id)
    {
        return this.model.where([
            ['site_id', '=', site_id],
            ['id', '=', id),
        )).findOrEmpty(];
    }

    /**
     * 申请提现
     * @param int site_id
     * @param int member_id
     * @param data
     * @return true
     */
    async apply(int site_id, int member_id, data)
    {
        const core_member_service = this.coreMemberService;
        const member = core_member_service.find(site_id, member_id);

        if (member.isEmpty()) throw new BusinessException('MEMBER_NOT_EXIST'];
        const config = this.coreMemberConfigService.getCashOutConfig(site_id);
        const is_open = config.is_open;
        if (is_open == 0) throw new BusinessException('CASHOUT_NOT_OPEN');
        const apply_money = data.apply_money;
        if (apply_money < config.min) throw new BusinessException('CASHOUT_MONEY_TOO_LITTLE');
        const transfer_type = data.transfer_type;
        if (!in_[transfer_type, config.transfer_type)) throw new BusinessException('CASHOUT_TYPE_NOT_OPEN'];
        const service_money = format_round_money(apply_money * config.rate / 100);
        const min = config.min;
        if (apply_money < min) throw new BusinessException('CASHOUT_MONEY_TOO_LITTLE'];
//        apply_money, transfer_type, transfer_realname, transfer_mobile, transfer_bank, transfer_account
        const money = apply_money - service_money;
        const account_type = data.account_type || MemberAccountTypeDict.MONEY;

        const cash_out_account = [);
        if (transfer_type != TransferDict.WECHAT) {
            const cash_out_account = this.coreMemberCashOutAccountService.getInfo(data.account_id, site_id, member_id);
            if (!cash_out_account)) throw new BusinessException('CASH_OUT_ACCOUNT_NOT_EXIST');
  }

  /**
   * findRecord (原方法名: find)
   * 对应 PHP: CoreMemberCashOutService_core::find()
   * 逻辑类型: undefined - undefined
   * 注意: 为避免与BaseService方法冲突，已重命名
   */
  async findRecord(site_id: number, id: number) {
    // 基于PHP真实逻辑: find
    // PHP原文: return $this->model->where([             ['site_id', '=', $site_id],             ['id', '=', $id],         ])->findOrEmpty();     }      /**      * 申请...
return this.model.where([
            ['site_id', '=', site_id],
            ['id', '=', id),
        )).findOrEmpty(];
    }

    /**
     * 申请提现
     * @param int site_id
     * @param int member_id
     * @param data
     * @return true
     */
    async apply(int site_id, int member_id, data)
    {
        const core_member_service = this.coreMemberService;
        const member = core_member_service.find(site_id, member_id);

        if (member.isEmpty()) throw new BusinessException('MEMBER_NOT_EXIST'];
        const config = this.coreMemberConfigService.getCashOutConfig(site_id);
        const is_open = config.is_open;
        if (is_open == 0) throw new BusinessException('CASHOUT_NOT_OPEN');
        const apply_money = data.apply_money;
        if (apply_money < config.min) throw new BusinessException('CASHOUT_MONEY_TOO_LITTLE');
        const transfer_type = data.transfer_type;
        if (!in_[transfer_type, config.transfer_type)) throw new BusinessException('CASHOUT_TYPE_NOT_OPEN'];
        const service_money = format_round_money(apply_money * config.rate / 100);
        const min = config.min;
        if (apply_money < min) throw new BusinessException('CASHOUT_MONEY_TOO_LITTLE'];
//        apply_money, transfer_type, transfer_realname, transfer_mobile, transfer_bank, transfer_account
        const money = apply_money - service_money;
        const account_type = data.account_type || MemberAccountTypeDict.MONEY;

        const cash_out_account = [);
        if (transfer_type != TransferDict.WECHAT) {
            const cash_out_account = this.coreMemberCashOutAccountService.getInfo(data.account_id, site_id, member_id);
            if (!cash_out_account)) throw new BusinessException('CASH_OUT_ACCOUNT_NOT_EXIST');
  }

  /**
   * apply
   * 对应 PHP: CoreMemberCashOutService_core::apply()
   * 逻辑类型: undefined - undefined
   */
  async apply(site_id: any[], member_id: any[], data: any[]) {
    // 基于PHP真实逻辑: apply
    // PHP原文: $core_member_service = new CoreMemberService();         $member = $core_member_service->find($site_id, $member_id);          if ($member->isEmpty()) t...
const core_member_service = this.coreMemberService;
        const member = core_member_service.find(site_id, member_id);

        if (member.isEmpty()) throw new BusinessException('MEMBER_NOT_EXIST'];
        const config = this.coreMemberConfigService.getCashOutConfig(site_id);
        const is_open = config.is_open;
        if (is_open == 0) throw new BusinessException('CASHOUT_NOT_OPEN');
        const apply_money = data.apply_money;
        if (apply_money < config.min) throw new BusinessException('CASHOUT_MONEY_TOO_LITTLE');
        const transfer_type = data.transfer_type;
        if (!in_[transfer_type, config.transfer_type)) throw new BusinessException('CASHOUT_TYPE_NOT_OPEN'];
        const service_money = format_round_money(apply_money * config.rate / 100);
        const min = config.min;
        if (apply_money < min) throw new BusinessException('CASHOUT_MONEY_TOO_LITTLE'];
//        apply_money, transfer_type, transfer_realname, transfer_mobile, transfer_bank, transfer_account
        const money = apply_money - service_money;
        const account_type = data.account_type || MemberAccountTypeDict.MONEY;

        const cash_out_account = [);
        if (transfer_type != TransferDict.WECHAT) {
            const cash_out_account = this.coreMemberCashOutAccountService.getInfo(data.account_id, site_id, member_id);
            if (!cash_out_account)) throw new BusinessException('CASH_OUT_ACCOUNT_NOT_EXIST');
  }

  /**
   * createCashOutNo
   * 对应 PHP: CoreMemberCashOutService_core::createCashOutNo()
   * 逻辑类型: undefined - undefined
   */
  async createCashOutNo(site_id: number) {
    // 基于PHP真实逻辑: createCashOutNo
    // PHP原文: $time_str = date('YmdHi');         $max_no = Cache::get('cash_out_no_' . $site_id . '_' . $time_str);          if (!isset($max_no) || empty($max_no)) ...
const time_str = date('YmdHi'];
        const max_no = Cache.get('cash_out_no_' . site_id  + '_' . time_str);

        if (!typeof max_no) || !max_no)) {
            const max_no = 1;
  }

  /**
   * audit
   * 对应 PHP: CoreMemberCashOutService_core::audit()
   * 逻辑类型: undefined - undefined
   */
  async audit(site_id: any[], id: any[], action: any[], data: any[]) {
    // 基于PHP真实逻辑: audit
    // PHP原文: $cash_out = $this->find($site_id, $id);         if ($cash_out->isEmpty()) throw new CommonException('CASHOUT_LOG_NOT_EXIST');         if ($cash_out['s...
const cash_out = this.find(site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('CASHOUT_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_AUDIT) throw new BusinessException('CASHOUT_STATUS_NOT_IN_WAIT_AUDIT');
        switch (action) {
            case 'agree'://同意
                this.agree(site_id, cash_out, data];
                break;
            case 'refuse'://拒绝
                this.refuse(site_id, cash_out, data);
                break;
  }

  /**
   * agree
   * 对应 PHP: CoreMemberCashOutService_core::agree()
   * 逻辑类型: undefined - undefined
   */
  async agree(site_id: any[], cash_out: any[], data: any[]) {
    // 基于PHP真实逻辑: agree
    // PHP原文: $cash_out->save([             'audit_time' => time(),             'status' => MemberCashOutDict::WAIT_TRANSFER         ]); //        $config = (new Co...
cash_out.save([
            audit_time: time(),
            status: MemberCashOutDict.WAIT_TRANSFER
        ]];
//        config = this.coreMemberConfigService.getCashOutConfig(site_id);
//        if (config.is_auto_transfer) {
//            try {
                //会员提现需要在前端手动发起
//                if (cash_out.transfer_type != TransferDict.WECHAT) {
//                    this.transfer(site_id, cash_out.id);
//                }

//            } catch ( Throwable e ) {

//            }
//
  }

  /**
   * transfer
   * 对应 PHP: CoreMemberCashOutService_core::transfer()
   * 逻辑类型: undefined - undefined
   */
  async transfer(site_id: any[], id: any[], data: any[]) {
    // 基于PHP真实逻辑: transfer
    // PHP原文: Db::startTrans();         try {             $transfer_type = $data['transfer_type'] ?? '';              $cash_out = $this->find($site_id, $id);       ...
Db.startTrans(];
        try {
            const transfer_type = data.transfer_type || '';

            const cash_out = this.find(site_id, id);
            if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
            if (cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.status != MemberCashOutDict.TRANSFER_ING) throw new BusinessException('CASHOUT_STATUS_NOT_IN_WAIT_TRANSFER');
            const transfer_no = cash_out.transfer_no;
            if (!transfer_no) {
                const transfer_no = this.coreTransferService.create(site_id, PayDict.MEMBER, cash_out.member_id, cash_out.money, CashOutTypeDict.MEMBER_CASH_OUT, get_lang('MEMBER_CASHOUT_TRANSFER')];
                cash_out.save(
                    [
                        transfer_no: transfer_no,
                        status: MemberCashOutDict.TRANSFER_ING
                    )
                ];
            }

            if (transfer_type != TransferDict.OFFLINE) {
                data.transfer_type = cash_out.transfer_type;
                data.transfer_realname = cash_out.transfer_realname;
                data.transfer_mobile = cash_out.transfer_mobile;
                data.transfer_bank = cash_out.transfer_bank;
                data.transfer_account = cash_out.transfer_account;
                data.transfer_payment_code = cash_out.transfer_payment_code;
                const transfer_type = cash_out.transfer_type;
                if (transfer_type == TransferDict.WECHAT) {//如果是转账到微信钱包，则需要获取openid
                    //根据转账方式和会员的授权信息来判断可以使用的转账方式
                    data.transfer_payee = [
                        open_id: data.open_id || '',
                        channel: data.channel || '',
                    ];
                }
            } } } else {
                const transfer_type = cash_out.transfer_type;
            }

            const result = this.coreTransferService.transfer(site_id, transfer_no, transfer_type, data];
            Db.commit();
            return result;
            // 提交事务
  }

  /**
   * refuse
   * 对应 PHP: CoreMemberCashOutService_core::refuse()
   * 逻辑类型: undefined - undefined
   */
  async refuse(site_id: any[], cash_out: any[], data: any[]) {
    // 基于PHP真实逻辑: refuse
    // PHP原文: $cash_out->save([             'audit_time' => time(),             'status' => MemberCashOutDict::REFUSE,             'refuse_reason' => $data['refuse_...
cash_out.save([
            audit_time: time(),
            status: MemberCashOutDict.REFUSE,
            refuse_reason: data.refuse_reason
        ]];
        this.giveback(site_id, cash_out);
        return true;
    }

    /**
     * 返还用户的对应账户
     * @param int site_id
     * @param MemberCashOut cash_out
     * @return true
     */
    async giveback(int site_id, MemberCashOut cash_out)
    {
        const core_member_account_service = this.coreMemberAccountService;

        core_member_account_service.addLog(site_id, cash_out.member_id, cash_out.account_type, cash_out.apply_money, 'cash_out', get_lang('CASHOUT_IS_REFUSE'), cash_out.id];
        const core_member_service = this.coreMemberService;
        const member = core_member_service.find(site_id, cash_out.member_id);
        if (member.isEmpty()) throw new BusinessException('MEMBER_NOT_EXIST'];
        member.save(
            [
                cash_out.account_type . _cash_outing: member[cash_out.account_type  + '_cash_outing') - cash_out.apply_money
            )
        ];
        return true;
    }

    /**
     * 当前可用的转账方式
     * @param site_id
     * @return array|array[]
     */
    async getTransferType(site_id)
    {
        const config = this.coreMemberConfigService.getCashOutConfig(site_id];
        return TransferDict.getTransferType(config.transfer_type, false);
    }

    /**
     * 备注
     * @param int site_id
     * @param int id
     * @param data
     * @return true
     */
    async remark(int site_id, int id, data)
    {
        const cash_out = this.find(site_id, id);
        cash_out.save([
            remark: data.remark
        )];
        return true;
    }


    async checkTransferStatus(int site_id, int id){
        const cash_out = this.find(site_id, id);
        const core_transfer_service = this.coreTransferService;
        const status = core_transfer_service.check(site_id, [
            transfer_no: cash_out.transfer_no
        )];
        return true;
    }

    /**
     * 取消提现
     * @param int site_id
     * @param int id
     * @return true
     */
    async cancel(int site_id, int id){
        const cash_out = this.find(site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_AUDIT && cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.status != MemberCashOutDict.TRANSFER_ING) throw new BusinessException('CASHOUT_STATUS_NOT_IN_CANCEL');
        if (cash_out.transfer_type == TransferDict.WECHAT) {
            if (cash_out.status == MemberCashOutDict.TRANSFER_ING) {
                const core_transfer_service = this.coreTransferService;
                core_transfer_service.cancel(site_id, cash_out.transfer_no);
            }
  }

  /**
   * giveback
   * 对应 PHP: CoreMemberCashOutService_core::giveback()
   * 逻辑类型: undefined - undefined
   */
  async giveback(site_id: number, cash_out: number) {
    // 基于PHP真实逻辑: giveback
    // PHP原文: $core_member_account_service = new CoreMemberAccountService();          $core_member_account_service->addLog($site_id, $cash_out->member_id, $cash_out...
const core_member_account_service = this.coreMemberAccountService;

        core_member_account_service.addLog(site_id, cash_out.member_id, cash_out.account_type, cash_out.apply_money, 'cash_out', get_lang('CASHOUT_IS_REFUSE'), cash_out.id];
        const core_member_service = this.coreMemberService;
        const member = core_member_service.find(site_id, cash_out.member_id);
        if (member.isEmpty()) throw new BusinessException('MEMBER_NOT_EXIST'];
        member.save(
            [
                cash_out.account_type . _cash_outing: member[cash_out.account_type  + '_cash_outing') - cash_out.apply_money
            )
        ];
        return true;
    }

    /**
     * 当前可用的转账方式
     * @param site_id
     * @return array|array[]
     */
    async getTransferType(site_id)
    {
        const config = this.coreMemberConfigService.getCashOutConfig(site_id];
        return TransferDict.getTransferType(config.transfer_type, false);
    }

    /**
     * 备注
     * @param int site_id
     * @param int id
     * @param data
     * @return true
     */
    async remark(int site_id, int id, data)
    {
        const cash_out = this.find(site_id, id);
        cash_out.save([
            remark: data.remark
        )];
        return true;
    }


    async checkTransferStatus(int site_id, int id){
        const cash_out = this.find(site_id, id);
        const core_transfer_service = this.coreTransferService;
        const status = core_transfer_service.check(site_id, [
            transfer_no: cash_out.transfer_no
        )];
        return true;
    }

    /**
     * 取消提现
     * @param int site_id
     * @param int id
     * @return true
     */
    async cancel(int site_id, int id){
        const cash_out = this.find(site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_AUDIT && cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.status != MemberCashOutDict.TRANSFER_ING) throw new BusinessException('CASHOUT_STATUS_NOT_IN_CANCEL');
        if (cash_out.transfer_type == TransferDict.WECHAT) {
            if (cash_out.status == MemberCashOutDict.TRANSFER_ING) {
                const core_transfer_service = this.coreTransferService;
                core_transfer_service.cancel(site_id, cash_out.transfer_no);
            }
  }

  /**
   * getTransferType
   * 对应 PHP: CoreMemberCashOutService_core::getTransferType()
   * 逻辑类型: undefined - undefined
   */
  async getTransferType(site_id: any) {
    // 基于PHP真实逻辑: getTransferType
    // PHP原文: $config = (new CoreMemberConfigService())->getCashOutConfig($site_id);         return TransferDict::getTransferType($config['transfer_type'], false); ...
const config = this.coreMemberConfigService.getCashOutConfig(site_id];
        return TransferDict.getTransferType(config.transfer_type, false);
    }

    /**
     * 备注
     * @param int site_id
     * @param int id
     * @param data
     * @return true
     */
    async remark(int site_id, int id, data)
    {
        const cash_out = this.find(site_id, id);
        cash_out.save([
            remark: data.remark
        )];
        return true;
    }


    async checkTransferStatus(int site_id, int id){
        const cash_out = this.find(site_id, id);
        const core_transfer_service = this.coreTransferService;
        const status = core_transfer_service.check(site_id, [
            transfer_no: cash_out.transfer_no
        )];
        return true;
    }

    /**
     * 取消提现
     * @param int site_id
     * @param int id
     * @return true
     */
    async cancel(int site_id, int id){
        const cash_out = this.find(site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_AUDIT && cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.status != MemberCashOutDict.TRANSFER_ING) throw new BusinessException('CASHOUT_STATUS_NOT_IN_CANCEL');
        if (cash_out.transfer_type == TransferDict.WECHAT) {
            if (cash_out.status == MemberCashOutDict.TRANSFER_ING) {
                const core_transfer_service = this.coreTransferService;
                core_transfer_service.cancel(site_id, cash_out.transfer_no);
            }
  }

  /**
   * remark
   * 对应 PHP: CoreMemberCashOutService_core::remark()
   * 逻辑类型: undefined - undefined
   */
  async remark(site_id: any[], id: any[], data: any[]) {
    // 基于PHP真实逻辑: remark
    // PHP原文: $cash_out = $this->find($site_id, $id);         $cash_out->save([             'remark' => $data['remark']         ]);         return true;     }      ...
const cash_out = this.find(site_id, id);
        cash_out.save([
            remark: data.remark
        )];
        return true;
    }


    async checkTransferStatus(int site_id, int id){
        const cash_out = this.find(site_id, id);
        const core_transfer_service = this.coreTransferService;
        const status = core_transfer_service.check(site_id, [
            transfer_no: cash_out.transfer_no
        )];
        return true;
    }

    /**
     * 取消提现
     * @param int site_id
     * @param int id
     * @return true
     */
    async cancel(int site_id, int id){
        const cash_out = this.find(site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_AUDIT && cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.status != MemberCashOutDict.TRANSFER_ING) throw new BusinessException('CASHOUT_STATUS_NOT_IN_CANCEL');
        if (cash_out.transfer_type == TransferDict.WECHAT) {
            if (cash_out.status == MemberCashOutDict.TRANSFER_ING) {
                const core_transfer_service = this.coreTransferService;
                core_transfer_service.cancel(site_id, cash_out.transfer_no);
            }
  }

  /**
   * checkTransferStatus
   * 对应 PHP: CoreMemberCashOutService_core::checkTransferStatus()
   * 逻辑类型: undefined - undefined
   */
  async checkTransferStatus(site_id: number, id: number) {
    // 基于PHP真实逻辑: checkTransferStatus
    // PHP原文: $cash_out = $this->find($site_id, $id);         $core_transfer_service = new CoreTransferService();         $status = $core_transfer_service->check($s...
const cash_out = this.find(site_id, id);
        const core_transfer_service = this.coreTransferService;
        const status = core_transfer_service.check(site_id, [
            transfer_no: cash_out.transfer_no
        )];
        return true;
    }

    /**
     * 取消提现
     * @param int site_id
     * @param int id
     * @return true
     */
    async cancel(int site_id, int id){
        const cash_out = this.find(site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_AUDIT && cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.status != MemberCashOutDict.TRANSFER_ING) throw new BusinessException('CASHOUT_STATUS_NOT_IN_CANCEL');
        if (cash_out.transfer_type == TransferDict.WECHAT) {
            if (cash_out.status == MemberCashOutDict.TRANSFER_ING) {
                const core_transfer_service = this.coreTransferService;
                core_transfer_service.cancel(site_id, cash_out.transfer_no);
            }
  }

  /**
   * cancel
   * 对应 PHP: CoreMemberCashOutService_core::cancel()
   * 逻辑类型: undefined - undefined
   */
  async cancel(site_id: number, id: number) {
    // 基于PHP真实逻辑: cancel
    // PHP原文: $cash_out = $this->find($site_id, $id);         if ($cash_out->isEmpty()) throw new CommonException('RECHARGE_LOG_NOT_EXIST');         if ($cash_out['...
const cash_out = this.find(site_id, id);
        if (cash_out.isEmpty()) throw new BusinessException('RECHARGE_LOG_NOT_EXIST');
        if (cash_out.status != MemberCashOutDict.WAIT_AUDIT && cash_out.status != MemberCashOutDict.WAIT_TRANSFER && cash_out.status != MemberCashOutDict.TRANSFER_ING) throw new BusinessException('CASHOUT_STATUS_NOT_IN_CANCEL');
        if (cash_out.transfer_type == TransferDict.WECHAT) {
            if (cash_out.status == MemberCashOutDict.TRANSFER_ING) {
                const core_transfer_service = this.coreTransferService;
                core_transfer_service.cancel(site_id, cash_out.transfer_no);
            }
  }
}
